package com.cheung.leetcode.greedy;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.greedy
 * @Author :  cheungming
 * @CreateTime : 2024-05-31 08:36:37
 * @Description : 摆动序列
 * @Version : 1.0
 */
public class Code376Test {

    public int wiggleMaxLength(int[] nums) {
        if (nums.length < 2) {
            return nums.length;
        }
        int curDiff = 0;
        int preDiff = 0;
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            curDiff = nums[i] - nums[i - 1];
            if (curDiff > 0 && preDiff <= 0 || curDiff < 0 && preDiff >= 0) {
                count++;
                preDiff = curDiff;
            }
        }

        return count;
    }

    @Test
    public void test1() {
        int[] nums = new int[]{1, 7, 4, 9, 2, 5};
        assert wiggleMaxLength(nums) == 6;
    }

    @Test
    public void test2() {
        int[] nums = new int[]{1, 17, 5, 10, 13, 15, 10, 5, 16, 8};
        assert wiggleMaxLength(nums) == 7;
    }

    @Test
    public void test3() {
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        assert wiggleMaxLength(nums) == 2;
    }
}
